//
//  DiReSettingViewController.m
//  OutesIOS
//
//  Created by norman on 2018/11/12.
//  Copyright © 2018 norman. All rights reserved.
//

#import "DiReSettingViewController.h"
#import "TPKeyboardAvoidingTableView.h"
#import "EditorTableViewCell.h"
#import "DBManager.h"
#import "HttpTool.h"


@interface DiReSettingViewController ()

@property (nonatomic,strong) TPKeyboardAvoidingTableView *tableView;
@property (nonatomic,strong) NSMutableDictionary *settingData;
@property (nonatomic,strong) NSArray *selectionData0;
@property (nonatomic,strong) NSArray *selectionData1;
@property (nonatomic,strong) NSArray *selectionData2;


@end

@implementation DiReSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"地热盘管设置";
//    [self initData];
    [self setupViews];
    // Do any additional setup after loading the view.
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self updateSetting];
//    [Tools saveApplicationData:self.settingData forKey:DiReSetting];
}

- (void)updateSetting {
    //参数
    if (self.settingData == nil) {
        return;
    }
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:[self.settingData objectForKey:@"bwc_method"] forKey:@"bwc_method"];
    [params setValue:[self.settingData objectForKey:@"bwc_method_id"] forKey:@"bwc_method_id"];
    [params setValue:[self.settingData objectForKey:@"dmc_type"] forKey:@"dmc_type"];
    [params setValue:[self.settingData objectForKey:@"dmc_type_id"] forKey:@"dmc_type_id"];
    [params setValue:[self.settingData objectForKey:@"jrg_type"] forKey:@"jrg_type"];
    [params setValue:[self.settingData objectForKey:@"jrg_type_id"] forKey:@"jrg_type_id"];
    [params setValue:[self.settingData objectForKey:@"pg_d"] forKey:@"pg_d"];
    [params setValue:[self.settingData objectForKey:@"pg_cut_length"] forKey:@"pg_cut_length"];
    
    [HttpClient postWithUrl:HTTP_POST_SETTING_DRPG token:[AccountManager manager].token paramDic:params resultClass:[CommonResponse class] success:^(CommonResponse *obj) {
    } failure:^(NSError *error) {
    }];
}

- (void)setupViews{
    //列表
    self.tableView = [[TPKeyboardAvoidingTableView alloc] initWithFrame:[UIView frameWithOutNav] style:UITableViewStylePlain];
    self.tableView.backgroundColor = CM_TABLE_BG_COLOR;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    //下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    [self.tableView.mj_header beginRefreshing];
}

- (void)loadData {
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setValue:[AccountManager manager].token forKey:@"token"];
    [HttpTool get:HTTP_GET_SETTING_INFO params:param success:^(NSDictionary *responseObj) {
        [self.tableView.mj_header endRefreshing];
        self.settingData = [responseObj mutableCopy];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
    }];
}

#pragma mark - tableView代理
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.settingData != nil) {
        return 5;
    }
    return 0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 4) {
        EditorTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
        if (cell == nil) {
            cell = [[EditorTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell1"];
        }
        [cell setTitleStr:@"自动拆分长度" andColor:nil andHintStr:@"请输入" andInputViewIsHidden:NO andInfoStr:[NSString stringWithFormat:@"%@", [self.settingData objectForKey:@"pg_cut_length"]] andUnit:@"m"];
        if ([[self.settingData objectForKey:@"pg_cut_length"] intValue] > 0) {
             [cell setEditorText:[NSString stringWithFormat:@"%@", [self.settingData objectForKey:@"pg_cut_length"]]];
        }
        cell.TextFiledValueBlock = ^(NSString *value) {
            [self.settingData setValue:value forKey:@"pg_cut_length"];
        };
        [cell setKeyboardType:UIKeyboardTypeNumberPad];
        [cell setItemIsNesessory];
        return cell;
    } else {
        UITableViewCell *Selectcell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"SelectArea_Cell"];
        Selectcell.textLabel.textColor = CM_TABLE_TITLE_COLOR;
        Selectcell.detailTextLabel.textColor = CM_TABLE_INFO_COLOR;
        Selectcell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        if (indexPath.row == 0) {
            Selectcell.textLabel.text = @"保温层做法";
            if ([Tools isEmpty:[self.settingData objectForKey:@"bwc_method"]]) {
                Selectcell.detailTextLabel.text = @"请选择";
            } else {
                Selectcell.detailTextLabel.text = [self.settingData objectForKey:@"bwc_method"];
            }
        } else if (indexPath.row == 1) {
            Selectcell.textLabel.text = @"地面层";
            if ([Tools isEmpty:[self.settingData objectForKey:@"dmc_type"]]) {
                Selectcell.detailTextLabel.text = @"请选择";
            } else {
                Selectcell.detailTextLabel.text = [self.settingData objectForKey:@"dmc_type"];
            }
        } else if (indexPath.row == 2) {
            Selectcell.textLabel.text = @"加热管类型";
            if ([Tools isEmpty:[self.settingData objectForKey:@"jrg_type"]]) {
                Selectcell.detailTextLabel.text = @"请选择";
            } else {
                Selectcell.detailTextLabel.text = [self.settingData objectForKey:@"jrg_type"];
            }
        } else {
            Selectcell.textLabel.text = @"加热管管径(mm)";
            if ([[self.settingData objectForKey:@"pg_d"] intValue] == 0) {
                Selectcell.detailTextLabel.text = @"请选择";
            } else {
                Selectcell.detailTextLabel.text = [NSString stringWithFormat:@"%@", [self.settingData objectForKey:@"pg_d"]];
            }
        }
        return Selectcell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row < 4) {
        [self selectionOperate:indexPath.row];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"cell1";
    EditorTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[EditorTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    return [cell getHeight];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreen_Width, DEFAULT_SECTION_HEIGHT)];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return DEFAULT_SECTION_HEIGHT;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreen_Width, CGFLOAT_MIN)];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}

- (void)selectionOperate:(NSUInteger)row {
    NSArray *data;
    NSString *title;
    if (row == 0) {
        title = @"保温层做法";
        data = [self.settingData objectForKey:@"bwc_methods"];
    } else if (row == 1) {
        title = @"地面层";
        data = [self.settingData objectForKey:@"dmc_types"];
    } else if (row == 2) {
        title = @"加热管类型";
        data = [self.settingData objectForKey:@"jrg_types"];
    } else {
        title = @"选择加热管管径(mm)";
        data = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObject:@"12" forKey:@"Name"], [NSDictionary dictionaryWithObject:@"16" forKey:@"Name"], [NSDictionary dictionaryWithObject:@"20" forKey:@"Name"], nil];
    }
    if (data == nil || data.count == 0) {
        return;
    }
        
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle: nil message: nil preferredStyle:UIAlertControllerStyleActionSheet];
    //修改title
    NSMutableAttributedString *alertControllerStr = [[NSMutableAttributedString alloc] initWithString:title];
    [alertControllerStr addAttribute:NSForegroundColorAttributeName value:BSBlackTextColor range:NSMakeRange(0, title.length)];
    [alertControllerStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:18] range:NSMakeRange(0, title.length)];
    [alertController setValue:alertControllerStr forKey:@"attributedTitle"];

    for (NSDictionary *item in data) {
        [alertController addAction: [UIAlertAction actionWithTitle:[item objectForKey:@"Name"] style: UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            if (row == 0) {
                [self.settingData setObject:[item objectForKey:@"Name"] forKey:@"bwc_method"];
                [self.settingData setObject:[item objectForKey:@"Id"] forKey:@"bwc_method_id"];
            } else if (row == 1) {
                [self.settingData setObject:[item objectForKey:@"Name"] forKey:@"dmc_type"];
                [self.settingData setObject:[item objectForKey:@"Id"] forKey:@"dmc_type_id"];
            } else if (row == 2) {
                [self.settingData setObject:[item objectForKey:@"Name"] forKey:@"jrg_type"];
                [self.settingData setObject:[item objectForKey:@"Id"] forKey:@"jrg_type_id"];
            } else {
                if ([[item objectForKey:@"Name"] integerValue] == 12) {
                    [self.settingData setValue:@"50" forKey:@"pg_cut_length"];
                } else if ([[item objectForKey:@"Name"] integerValue] == 16) {
                    [self.settingData setValue:@"80" forKey:@"pg_cut_length"];
                } else {
                    [self.settingData setValue:@"100" forKey:@"pg_cut_length"];
                }
                [self.settingData setObject:[item objectForKey:@"Name"] forKey:@"pg_d"];
            }
            if (row == 3) {
                [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForItem:3 inSection:0], [NSIndexPath indexPathForItem:4 inSection:0], nil] withRowAnimation:UITableViewRowAnimationAutomatic];
            } else {
                [self.tableView reloadRowsAtIndexPaths:indexPathObj(row, 0) withRowAnimation:UITableViewRowAnimationAutomatic];
            }
            
        }]];
    }
    [alertController addAction: [UIAlertAction actionWithTitle: @"取消" style: UIAlertActionStyleCancel handler:nil]];
    [self presentViewController: alertController animated: YES completion: nil];
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
